﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using CakeAnimation;

namespace CharacterAnimationEditor
{
    public class CurveGUI
    {
        Texture2D CircleTexture;

        public CurveGUI(Context context)
        {

            CircleTexture = context.Content.Load<Texture2D>("Circle");

        }

        public void Update(Context context)
        {


        }

        public void Draw(Context context)
        {
            Bone activeBone = context.SelectedBone;
            if (activeBone == null)
            {
                activeBone = context.HoveredBone;
                if (activeBone == null)
                {
                    return;
                }
            }

            float min = float.MaxValue;
            float max = float.MinValue;

            foreach (var kf in context.Animation.m_Keyframes)
            {
                Bone b = kf.Pose.GetBone(activeBone.Name);
                min = Math.Min(min, b.LocalRotation);
                max = Math.Max(max, b.LocalRotation);
            }

            max = Math.Abs(max);

            foreach (var kf in context.Animation.m_Keyframes)
            {

                Bone b = kf.Pose.GetBone(activeBone.Name);
                float timescale = 400 / context.Animation.GetDuration();
                float a =  (b.LocalRotation - min) / (max-min);

                Vector2 pos = new Vector2(
                    100 + kf.Time * timescale,
                    100 + MathHelper.Lerp(0, 50, a));

                context.SpriteBatch.Draw(
                    CircleTexture,
                    pos,
                    kf == context.CurrentKeyFrame ? Color.Yellow : Color.Gray);
            }

        }
    }
}
